using System;
using System.Collections.Generic;
using System.Drawing;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.Relationship
{
    /// <summary>
    /// This wizard step is the second step for creating a new <see cref="DMRelationship"/>. 
    /// This step allows the user to select whether they want to create the Foreign Key Props or not.
    /// And to select the Cardinality of the Relationship
    /// This is only required when the system did not find the relevant Foreign Key Props.
    /// </summary>
    public class CreateRelationshipWizardStep4 : WizardStepWin
    {
        private readonly ErrorViewerControl _errorViewerControl;
        private readonly ILabel _label;
        private DMRelationshipCreator RelationshipCreator { get; set; }

        private IControlFactory ControlFactory { get; set; }

        public CreateRelationshipWizardStep4(DMRelationshipCreator relationshipCreator,
                                             IControlFactory controlFactory)
        {
            if (relationshipCreator == null) throw new ArgumentNullException("relationshipCreator");
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            if (relationshipCreator.Relationship == null)
                new HabaneroArgumentException("relationshipCreator.Relationship",
                        "You cannot CreateRelationshipWizardStep4 if the "
                        + "RelationshipCreator does not have a relationship");
            RelationshipCreator = relationshipCreator;
            ControlFactory = controlFactory;
            BorderLayoutManager layoutManager = this.ControlFactory.CreateBorderLayoutManager(this);
            _label = ControlFactory.CreateLabel();
            _label.Height = 80;
            _errorViewerControl = new ErrorViewerControl();
            layoutManager.AddControl(_label, BorderLayoutManager.Position.North);
            layoutManager.AddControl(_errorViewerControl, BorderLayoutManager.Position.Centre);
        }

        public DMRelationship Relationship
        {
            get
            {
                return this.RelationshipCreator == null ? null : this.RelationshipCreator.Relationship;
            }
        }

        public override string HeaderText
        {
            get { return "Step4: Confirm Create Relationship"; }
        }
        private static void AddErrorList(IEnumerable<IBOError> tmpErrors, List<IBOError> errors)
        {
            errors.AddRange(tmpErrors);
        }

        public override void InitialiseStep()
        {
            var message = "You are creating a relationship '" + this.Relationship.RelationshipName + "' between '" +
            this.Relationship.OwnerClass.ClassNameBO + "' and '" + this.Relationship.RelatedClass.ClassNameBO;
            List<IBOError> errors = new List<IBOError>();
            IList<IBOError> tmpErrors;
            foreach (var relProp in this.Relationship.RelationshipProperties)
            {
                relProp.Status.HasWarnings(out tmpErrors);
                AddErrorList(tmpErrors, errors);
            }
            this.Relationship.Status.HasWarnings(out tmpErrors);
            AddErrorList(tmpErrors, errors);
            if (errors.Count > 0)
            {
                _label.Text = message + " this relationship will be created but there are a number of errors that must be resolved before you will be able to generate code";
                _errorViewerControl.ErrorList = errors;
                _errorViewerControl.Visible = true;
                return;
            }
            _label.Text = message;
            _errorViewerControl.Visible = false;
        }

    }
}